<template>
	<view class="car-rental">
		<u-popup 
			:show="show"
			mode="center"
			@close="show=false"
			:safeAreaInsetTop="true"
			:closeable="true" round="10"
			:safeAreaInsetBottom="false"
			:closeOnClickOverlay="false"
		>
		<picker @change="gainSelectStore" :value="current" :range="store_list" mode = 'selector'>
			<view class="uni-input">{{store_list[current].name}}</view>
		</picker>
		
		<view class="content">
			<view class="location">
				<block v-if="gain_info.type==1">
					<view class="city" v-if="gain_info.city!=''">
						<view class="location-desc">
							<view class="dot"></view>
							<text class="text">城市</text>
						</view>
						<text class="city-text">{{gain_info.city}}</text>
					</view>
					<view class="address" @click="openAddress('gain')" :class="gain_info.title==''?'no-select':''">
						<text class="location-desc">取车地址</text>
						<text class="address-text">{{gain_info.title || "请选择取车地址"}}</text>
					</view>
				</block>
				<block v-if="gain_info.type==2">
					<view class="city" @click="cityListShow(1)">
						<view class="location-desc">
							<view class="dot"></view>
							<text class="text">选择城市</text>
						</view>
						<text class="city-text">{{gain_info.city || ''}}</text>
					</view>
					<view class="address" @click="gainStoreShow" :class="gain_info.storeName==''?'no-select':''">
						<text class="location-desc">取车门店</text>
						<text class="address-text">{{gain_info.storeName || "请选择取车门店"}}</text>
					</view>
				</block>
				<view class="city" style="margin-right: 0rpx;">
					<view class="location-desc">
						<view class="dot"></view>
						<text class="text">异地还车</text>
					</view>
					<block>
						<u-switch v-model="back_car_show" :activeColor="$s.theme" size="20" @change="yidiChange"></u-switch>
					</block>
				</view>
			</view>
			
			<view class="location revert" v-if="back_car_show">
				<block v-if="back_info.type==1">
					<view class="city" v-if="back_info.city!=''">
						<view class="location-desc">
							<view class="dot"></view>
							<text class="text">城市</text>
						</view>
						<text class="city-text">{{back_info.city}}</text>
					</view>
					<view class="address" @click="openAddress('back')" :class="back_info.title==''?'no-select':''">
						<text class="location-desc">还车地址</text>
						<text class="address-text">{{back_info.title || "请选择还车地址"}}</text>
					</view>
				</block>
				<block v-if="back_info.type==2">
					<view class="city" @click="cityListShow(2)">
						<view class="location-desc">
							<view class="dot"></view>
							<text class="text">选择城市</text>
						</view>
						<text class="city-text">{{back_info.city || ''}}</text>
					</view>
					<view class="address" @click="backStoreShow" :class="back_info.storeName==''?'no-select':''">
						<text class="location-desc">还车门店</text>
						<text class="address-text">{{back_info.storeName || "请选择还车门店"}}</text>
					</view>
				</block>
			</view>
			
			<view class="location revert checkbox-tr">
				<view>
					<u-checkbox-group placement="row" v-model="type_checkbox" @change="gainBackChange">
						<!-- 开启异地还车 -->
						<block v-if="back_car_show">
							<view class="u-checkbox-mrg">
							<u-checkbox
								name="gain"
								:activeColor="$s.theme"
								:label="gain_info.type==1?'送车上门':'门店取车'"
								class="checkbox-margin"
							></u-checkbox>
							</view>
							<u-checkbox
								name="back"
								:activeColor="$s.theme2"
								:label="back_info.type==1?'上门收车':'门店还车'"
							></u-checkbox>
						</block>
						<!-- 关闭异地还车 -->
						<block v-else>
							<u-checkbox
								name="gain"
								:activeColor="$s.theme"
								:label="gain_info.type==1?'上门送取车':'门店取还车'"
								class="checkbox-margin"
							></u-checkbox>
						</block>
					</u-checkbox-group>
				</view>
				<view>
					<u-checkbox-group placement="row" @change="daijiaChange">
						<u-checkbox
							name="daijia"
							activeColor="#F7780E"
							label="代驾"
							:checked="is_drive"
						></u-checkbox>
					</u-checkbox-group>
				</view>
			</view>
			
			<view @click="openDate()" class="time-range">
				<view class="range rent">
					<text class="label">取车时间</text>
					<text class="day-time">{{start_date.timestamp | date('mm月dd日 hh:MM')}}</text>
				</view>
				<view class="diff-day">
					{{diff_day}}天
				</view>
				<view class="range revert">
					<text class="label">还车时间</text>
					<text class="day-time">{{end_date.timestamp | date('mm月dd日 hh:MM')}}</text>
				</view>
			</view>
			
			<view class="buttons">
				<view class="find-car">
					<button class="my-but-primary" @click="findCar()" :disabled="button_disable">确定修改</button>
				</view>
			</view>
		</view>
	</u-popup>

	<my-picker :show="gain_store_show" :confirmColor="$s.theme" :columns="[gain_store_list]" keyName="name" @cancel="gainStoreClose" @confirm="gainSelectStore"></my-picker>
	<my-picker :show="back_store_show" :confirmColor="$s.theme" :columns="[back_store_list]" keyName="name" @cancel="backStoreClose" @confirm="backSelectStore"></my-picker>
	
	<search-picker :show="city_list_show" :confirmColor="$s.theme" :columns="city_list" keyName="name" @cancel="cityListClose" @confirm="selectCity"></search-picker>
	
	<calendar-select
		ref="calendar"
		@dateConfirm="dateConfirm"
		@close="show=true"
		:start="start_date"
		:end="end_date"
		:setvuex="false"
		:carid="carid"
	></calendar-select>
	
	</view>
</template>
<script>
	import { mapState } from 'vuex'
	export default {
		props: {
			carid:{
				type:[Number],
				default:0
			}
		},
		data() {
			return {
				show:false,
				gain_title:"门店取车",
				back_title:"门店还车",
				gain_info: { 				//取车地址信息
					cityId: 0,
					city: '',
					title: '',
					address: '',
					type: 0,
					store: 0,
					storeName: '',
					screenStore: '', 		//用于筛选坐标的车辆
					lat: 0,
					lng: 0,
				},
				back_info: { 				//还车地址信息
					cityId: 0,
					city: '',
					title: '',
					address: '',
					type: 0,
					store: 0,
					storeName: '',
					screenStore: '', 		//用于筛选坐标的车辆
					lat: 0,
					lng: 0,
				},
				type_checkbox:[],					//取还车方式
				ls_gain_type:0,						//取车方式	3为可选，其他为不可选
				ls_back_type:0,						//还车方式	3为可选，其他为不可选
				back_text:'',						//还车门店文字
				gain_store_show:false,				//取车门店列表弹窗开关
				back_store_show:false,				//还车门店列表弹窗开关
				city_list_show:false,				//取车城市列表弹窗开关
				city_type:1,						//城市列表弹窗类型 1 gain 2 back
				city_list:[],						//城市列表
				gain_store_list:[],					//取车门店列表
				back_store_list:[],                 //还车门店列表
				start_date:{},						//取车时间
				end_date:{},						//还车时间
				list: [],
				back_car_show:false,				//是否开启还车选项
				button_disable:true,				//按钮是否可点击
				is_drive:false,						//是否开启代驾
				diff_day:0, 						//租车天数
			}
		},
		watch: {
		},
		computed:{
			...mapState({
				city:state => state.$defaultCity,
				$gain_info:state => state.$gainInfo,
				$back_info:state => state.$backInfo,
				$start_date:state => state.$startDate,
				$end_date:state => state.$endDate,
				$diff_day:state => state.$diffday,
				$is_drive:state => state.$isDrive,
				$back_car_show:state => state.$backCarShow,
				is_login:state => state.$isLogin,
				user_info:state => state.$userInfo,
			})
		},
		mounted(){
		},
		onReady(){
			
		},
		methods: {
			initData(){
			},
			// 打开弹窗
			open(){
				this.getRentalConfig()		//初始化数据
				this.show=true
			},
			close(){
				this.show=false
			},
			// 打开地图选择地址
			openAddress(type){
				let than=this
				uni.chooseLocation({
				    success: function (res) {
						uni.$u.api.mapCheck({lat:res.latitude,lng:res.longitude}).then(map_res=>{
							res.region=map_res.data.region
							
							if(type == 'gain'){
								than.gain_info.title = res.name
								than.gain_info.address = res.address
								than.gain_info.city = res.region
								than.gain_info.lat = res.latitude
								than.gain_info.lng = res.longitude
								than.gain_info.screenStore = map_res.data.screen_store
							}else if((type == 'back') || (!than.back_car_show && type=='gain')){
								// 如果关闭异地还车，则自动同步取车还车门店地址数据
								than.back_info.title = res.name
								than.back_info.address = res.address
								than.back_info.city = res.region
								than.back_info.lat = res.latitude
								than.back_info.lng = res.longitude
								than.back_info.screenStore = map_res.data.screen_store
								//获坐标附近门店ID，用于下一步筛选车辆
							}
						})
				    },
					fail: function (e){
						console.log(e);
						than.$c.error(e)
					}
				});
			},
			//初始化租车默认数据
			getRentalConfig(){
				uni.showLoading({
					title:"数据加载中..."
				})
				
				this.$u.api.homeConfig().then(res=>{
					res=res.data
					//门店列表
					this.store_list=[res.store_list]
					
					//租车时间
					this.start_date={...this.$start_date}
					this.end_date={...this.$end_date}
					this.diff_day=this.$diff_day
					
					//租车信息
					this.gain_info={...this.$gain_info}
					this.back_info={...this.$back_info}
					
					//代驾情况
					this.is_drive=this.$is_drive==1?true:false
					this.back_car_show=this.$back_car_show
					
					//取车还车方式复选框自动勾选
					if(this.gain_info.type==2 && !this.type_checkbox.includes('gain')){
						this.type_checkbox.push('gain')
					}
					if(this.back_info.type==2 && !this.type_checkbox.includes('back')){
						this.type_checkbox.push('back')
					}
					
					uni.hideLoading()
					this.button_disable=false;
				})
				
				// 获取城市列表
				uni.$u.api.mapLocation({
					lat:this.$gain_info.lat,
					lng:this.$gain_info.lng,
				}).then(res=>{
					// 城市列表
					this.city_list=[res.data.city_list];
				})
			},
			// 取车还车方式发现改变时
			gainBackChange(e){
				this.type_checkbox=e
				if(this.type_checkbox.includes('gain')){
					this.gain_info.type=2	//门店取车
				}else{
					this.gain_info.type=1	//送车上门
				}
				
				if(this.type_checkbox.includes('back')){
					this.back_info.type=2	//门店还车
				}else{
					this.back_info.type=1	//上门取车
				}
			},
			// 异地还车开关
			yidiChange(e){
				if(this.type_checkbox.includes('gain')){
					this.gainBackChange(['gain','back'])
				}else{
					this.gainBackChange([])
				}
			},
			// 代驾开关
			daijiaChange(e){
				this.is_drive=e.includes('daijia')?true:false
			},
			//选择取车门店弹窗
			gainSelectStore(e){
				this.gain_info.store=e.value[0].id
				this.gain_info.storeName=e.value[0].name
				this.gain_store_show=false
				this.show=true
				if(!this.back_car_show){		//如果关闭异地还车，则同步取车门店到还车门店数据
					this.backSelectStore(e)
				}
			},
			//显示取车门店弹窗
			gainStoreShow(){
				if(this.gain_info.cityId==0){
					this.$c.info('请先选择取车城市')
					return false;
				}else if(this.gain_store_list.length<=0){
					this.$c.info('该城市暂无取车门店！')
					return false;
				}else{
					this.gain_store_show=true
					this.show=false;
				}
			},
			//关闭取车门店弹窗
			gainStoreClose(){
				this.gain_store_show=false
				this.show=false
			},
			//选择还车门店弹窗
			backSelectStore(e){
				this.back_info.store=e.value[0].id
				this.back_info.storeName=e.value[0].name
				this.back_store_show=false
				this.show=true
			},
			//显示还车门店弹窗
			backStoreShow(){
				if(this.back_info.cityId==0){
					this.$c.info('请先选择还车城市')
					return false
				}else if(this.back_store_list.length<=0){
					this.$c.info('该城市暂无还车门店！')
					return false
				}else{
					this.back_store_show=true
					this.show=false
				}
			},
			//关闭还车门店弹窗
			backStoreClose(){
				this.back_store_show=false
				this.show=true
			},
			// 异地还车
			setBackCarShow(e){
				this.back_car_show=e
			},
			// 异地还车情况下切换 门店和选址
			setGainBackSwitch(e){
				if(e){					//门店取车
					this.gain_info.type=2
					this.back_info.type=2
				}else{					//送车上门
					this.gain_info.type=1
					this.back_info.type=1
				}
			},
			//城市列表弹窗
			cityListShow(e){
				if(this.city_list[0]){
					this.city_type=e
					this.city_list_show=true
					this.show=false
				}else{
					this.$c.info('没有可选城市')
				}
			},
			//关闭城市弹窗
			cityListClose(){
				this.city_list_show=false
				this.show=true;
			},
			//选择城市列表
			selectCity(e){
				let id=e.value[0].id
				let name=e.value[0].name
				this.$u.api.cityStoreList({id:id}).then(res=>{
					if(this.city_type==1){
						this.gain_info.cityId=id
						this.gain_info.city=name
						this.gain_store_list=res.data
						//重置取车门店
						this.gain_info.store=0;
						this.gain_info.storeName=''
					}else{
						this.back_info.cityId=id
						this.back_info.city=name
						this.back_store_list=res.data
						//重置还车门店
						this.back_info.store=0
						this.back_info.storeName=''
					}
				})
				this.city_list_show=false
				this.show=true
			},
			// 打开日期选择
			openDate(){
				this.show=false;
				this.$refs.calendar.dateOpen()
			},
			//确认借还车日期
			dateConfirm(e){
				this.start_date=e.start_date
				this.end_date=e.end_date
				this.diff_day=e.diff_day
				this.show=true
			},
			//确认提交
			findCar(){
				
				if(this.gain_info.type==2 && this.gain_info.store==0){
					this.$c.info('请选择取车门店')
					return false
				}
				if(this.back_info.type==2 && this.back_info.store==0 && this.back_car_show){
					this.$c.info('请选择还车门店')
					return false
				}
				
				this.submitData()
			},
			// 提交表单数据
			submitData(){
				//如果没有开启异地还车，则还车数据自动同步取车数据
				if(!this.back_car_show) this.back_info=this.gain_info
				
				this.$store.$gain_info=this.gain_info
				this.$store.commit('setStartDate',this.start_date)
				this.$store.commit('setEndDate',this.end_date)
				this.$store.commit('setDaijia',this.is_drive)
				this.$store.commit('setDiffDay',this.diff_day)
				this.$store.commit('setBackCar',this.back_car_show)
				this.$store.commit('setGain',this.gain_info)
				this.$store.commit('setBack',this.back_info)
				
				this.show=false;
				this.$emit('submit')
			},
		}
	};
</script>
<style lang="scss">	
	.content {
	    z-index: 9;
	    display: block;
	    padding: 32rpx 40rpx 32rpx 30rpx;
	    overflow: hidden;
	    background-color: #fff;
	    border-bottom-right-radius: 20rpx;
	    border-bottom-left-radius: 20rpx;
	    transform: translateZ(0);
		border-radius: 30rpx;
		width: 700rpx;
	}
	
	.content .location,.content .time-range {
	    display: -ms-flexbox;
	    display: flex;
		flex-direction: row;
		justify-content: space-between;
	    border-bottom: 1px solid #f5f5f5;
	}
	
	.content .location {
	    position: relative;
	    padding: 6rpx 0rpx 24rpx 30rpx;
	}
	
	.content .location .location-desc {
	    display: -ms-flexbox;
	    display: flex;
		flex-direction: row;
	    -ms-flex-align: center;
	    align-items: center;
	    margin-bottom: 12rpx;
	    color: rgba(60,60,90,.6);
	    font-size: 24rpx;
	    line-height: 28rpx;
	}
	
	.content .location .location-desc .dot {
	    width: 10rpx;
	    height: 10rpx;
	    margin-right: 10rpx;
	    background: var(--view-theme);
	    border-radius: 50%;
	}
	
	.content .location .location-desc .text {
		font-size: 24rpx;
	    color: rgba(60,60,90,.6);
	}
	
	.content .location .location-desc{
		color:var(--view-theme);
	}
	
	.content .location.revert .location-desc{
		color:var(--view-assist);
	}
	
	.content .location .city {
	    min-width: 110rpx;
	    margin-right: 20rpx;
	    padding: 0 20rpx 0 10rpx;
	    background-size: 36rpx;
	}
	
	.content .location .city .city-text {
	    display: block;
	    font-weight: 700;
	    font-size: 28rpx;
	    line-height: 40rpx;
	}
	
	.content .location .address {
	    position: relative;
	    flex: 1;
	    width: calc(100% - 60rpx);
	    overflow: hidden;
	}
	.content .location .address.no-select .address-text {
		font-weight: 500;
		color: $u-tips-color;
	}
	
	.content .location .address .address-text {
	    display: block;
	    width: 100%;
	    padding-right: 16rpx;
	    overflow: hidden;
	    font-weight: 700;
	    font-size: 28rpx;
	    line-height: 40rpx;
	    white-space: nowrap;
	    text-overflow: ellipsis;
	}
	
	.content .location .address .address-text.placeholder {
	    color: #999;
	}
	
	.content .location .address .mask {
	    position: absolute;
	    top: 0;
	    right: 0;
	    bottom: 0;
	    z-index: 2;
	    width: 50rpx;
	    background: -webkit-gradient(linear,left top,right top,from(rgba(255,255,255,0)),to(#fff));
	    background: -webkit-linear-gradient(left,rgba(255,255,255,0),#fff);
	    background: -o-linear-gradient(left,rgba(255,255,255,0),#fff);
	    background: linear-gradient(to right,rgba(255,255,255,0),#fff);
	}
	
	.content .location .switch .switch-item {
	    position: relative;
	    top: 2rpx;
	    left: 4rpx;
	}
	
	.content .location.revert {
	    padding-top: 24rpx;
	}
	
	.content .location.revert .location-desc .dot {
	    background: var(--view-assist);
	}
	
	.content .time-range {
	    padding-left: 30rpx;
	    background-size: 36rpx;
		padding: 20rpx;
	}
	
	.content .time-range .range {
	    display: -ms-flexbox;
	    display: flex;
	    flex-direction: column;
	    -ms-flex-align: start;
	    align-items: flex-start;
	}
	
	.content .time-range .range.revert {
		
	}
	
	.content .time-range .diff-day {
		color: rgba(60,60,90,.6);
	    display: flex;
		justify-content: center;
		align-items: center;
	    font-size: 26rpx;
	}
	
	.content .time-range .range .label {
	    display: block;
	    margin-bottom: 8rpx;
	    padding-right: 30rpx;
	    color: rgba(60,60,90,.6);
	    font-size: 24rpx;
	    line-height: 28rpx;
	    background-image: url(@/static/images/icon-arrow-right-black.png);
	    background-repeat: no-repeat;
	    background-position: right center;
	    background-size: 30rpx auto;
	}
	
	.content .time-range .range .day-time {
	    display: block;
	    font-weight: 700;
	    font-size: 28rpx;
	    line-height: 40rpx;
	}
	
	.content .buttons {
	    display: -ms-flexbox;
	    display: flex;
		flex-direction: row;
	    margin-top: 32rpx;
	}
	
	.content .buttons .find-car {
	    position: relative;
	    flex: 1;
	}
	
	.content .buttons .find-car .huabei-free {
	    position: absolute;
	    top: -25rpx;
	    right: -10rpx;
	    z-index: 99;
	    display: block;
	    padding: 10rpx 14rpx;
	    color: #fff;
	    font-size: 18rpx;
	    line-height: 1;
	    background: #407bff;
	    border-radius: 30rpx;
	    pointer-events: none;
	}
	
	.content .buttons .find-car .huabei-free .arrow {
	    position: absolute;
	    bottom: -9rpx;
	    left: 20rpx;
	    display: block;
	    width: 35rpx;
	    height: 9rpx;
	    background-image: url(https://cdn.atzuche.com/mini/point-huabei-bg.png);
	    background-size: 100% 100%;
	}
	
	.content .buttons .fuli-btn,.content .mianya {
	    display: -ms-flexbox;
	    display: flex;
	    -ms-flex-align: center;
	    align-items: center;
	}
	
	.content .buttons .find-btn {
	    -ms-flex-pack: center;
	    justify-content: center;
	    height: 80rpx;
	    padding: 0 56rpx;
	    color: #fff;
	    font-size: 28rpx;
	    background: var(--view-theme);
	    border: 0;
	    border-radius: 50rpx 50rpx 50rpx 50rpx;
	}
	
	.content .buttons .fuli-btn {
	    -ms-flex-pack: center;
	    justify-content: center;
	    height: 80rpx;
	    padding: 0 56rpx;
	    color: #fff;
	    font-size: 30rpx;
	    background: #f2c94c;
	    border: 0;
	    border-radius: 10rpx 50rpx 50rpx 10rpx;
	}
	
	.content .buttons .fuli-btn.hover,.content .buttons .find-btn.hover {
	    opacity: .7;
	}
	
	.content .buttons .fuli-btn::after {
	    display: none;
	}
	
	.content .mianya {
	    width: 100%;
	    height: 64rpx;
	    margin-top: 32rpx;
	    padding: 0 28rpx;
	    color: #519bff;
	    font-size: 24rpx;
		flex-direction: row;
	    background-color: rgba(81,155,255,.05);
	    border-radius: 20rpx;
		justify-content: center;
	}
	.doem{
		border:2px solid #0081FF
	}
	.u-checkbox-mrg{
		margin-right: 20rpx;
	}
	.content .checkbox-tr{
		justify-content: space-between;
	}
</style>
